package com.example.learn_spring_boot3.controller;

import com.example.learn_spring_boot3.utils.ResponseData;
import com.example.learn_spring_boot3.entity.User;
import com.example.learn_spring_boot3.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userServeice;

    @PostMapping("/register")
    public ResponseEntity<ResponseData> register(@RequestBody User user) {
        return userServeice.register(user);
    }

    @PostMapping("/login")
    public ResponseEntity<ResponseData> login(@RequestBody User user) {
        return userServeice.login(user.getName(), user.getPassword());
    }

    @GetMapping("/info")
    public ResponseEntity<ResponseData> info(@RequestHeader("Authorization") String token) {
        return userServeice.info(token);
    }

    @GetMapping("/getList")
    public ResponseEntity<ResponseData> getList() {
        return userServeice.getList();
    }

    @GetMapping("/getListByPage")
    public ResponseEntity<ResponseData> getListByPage(@RequestParam Integer current, @RequestParam Integer size) {
        return userServeice.getListByPage(current, size);
    }

    @GetMapping("/getListByQuery")
    public ResponseEntity<ResponseData> getListByQuery(@RequestParam Integer sex, @RequestParam Integer age) {
        return userServeice.getListByQuery(sex, age);
    }

    @GetMapping("/getListAndOrders")
    public ResponseEntity<ResponseData> getListAndOrders() {
        return userServeice.getListAndOrders();
    }

    @GetMapping("/getListByCatce")
    public ResponseEntity<ResponseData> getListByCatce() {
        return userServeice.getListByCatch();
    }

    @GetMapping("/addTestData")
    public ResponseEntity<ResponseData> addTestData(@RequestParam int num) {
        return userServeice.addTestData(num);
    }

    @GetMapping("/get/{id}")
    public ResponseEntity<ResponseData> getUserById(@PathVariable int id) {
        return userServeice.getUserById(id);
    }


    @PostMapping("/add")
    public ResponseEntity<ResponseData> add(@RequestBody User user) {
        return userServeice.add(user);
    }

    @PostMapping("/addBatch")
    public ResponseEntity<ResponseData> addBatch(@RequestBody ArrayList<User> userList) {
        return userServeice.addBatch(userList);
    }

    @PostMapping("/update")
    public ResponseEntity<ResponseData> update(@RequestBody User user) {
        return userServeice.update(user);
    }

    @PostMapping("/delete")
    public ResponseEntity<ResponseData> delete(@RequestParam int id) {
        return userServeice.delete(id);
    }

    @GetMapping("/testTran")
    public ResponseEntity<ResponseData> testTran(@RequestParam int id) throws Exception {
        return userServeice.testTran(id);
    }
}